﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Portals;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Services.Localization;

namespace FortyFingers.Windows8Pin.Components
{
    public class Config
    {
        #region Standard code
        private Hashtable _settings = null;
        private int _moduleId = Null.NullInteger;
        private int _tabModuleId = Null.NullInteger;

        public Config(Hashtable settings, int moduleId, int tabModuleId)
        {
            _settings = settings;
            _moduleId = moduleId;
            _tabModuleId = tabModuleId;
        }

        private string GetDefault(string setting)
        {
            return Localization.GetString(setting, ConfigDefaultsResourceFile);
        }

        private int GetSettingInt(string setting, bool useDefault = true)
        {
            int i = Null.NullInteger;
            string settingValue = "";
            if (_settings.ContainsKey(setting))
            {
                settingValue = _settings[setting].ToString();
            }
            else if (useDefault)
            {
                settingValue = GetDefault(setting);
            }
            int.TryParse(settingValue, out i);

            return i;
        }

        private string GetSetting(string setting, bool useDefault = true)
        {
            string settingValue = "";
            if (_settings.ContainsKey(setting))
            {
                settingValue = _settings[setting].ToString();
            }
            else if (useDefault)
            {
                settingValue = GetDefault(setting);
            }

            return settingValue;
        }

        private PortalSettings Ps
        {
            get { return PortalSettings.Current; }
        }

        private ModuleController _moduleCtrl;
        private ModuleController ModuleCtrl
        {
            get
            {
                if (_moduleCtrl == null)
                {
                    _moduleCtrl = new ModuleController();
                }

                return _moduleCtrl;
            }
        }
        #endregion

        private const string ConfigDefaultsResourceFile = "~/DesktopModules/40Fingers/Windows8Pin/App_LocalResources/ConfigDefaults.resx";

        public string TileTitle
        {
            get
            {
                var retval = GetSetting("TileTitle", false);
                if (string.IsNullOrEmpty(retval))
                    retval = Ps.PortalName;

                return retval;
            }
            set { ModuleCtrl.UpdateModuleSetting(_moduleId, "TileTitle", value); }
        }

        public string TileBackground
        {
            get { return GetSetting("TileBackground", true); }
            set { ModuleCtrl.UpdateModuleSetting(_moduleId, "TileBackground", value); }
        }

        public string TileImageUrl
        {
            get 
            { 
                var retval = GetSetting("TileImageUrl", false);
                if(string.IsNullOrEmpty(retval))
                {
                    // get default: site logo
                    if(!String.IsNullOrEmpty(Ps.LogoFile))
                    {
                        retval = Ps.LogoFile;
                    }
                }
                return retval;
            }
            set { ModuleCtrl.UpdateModuleSetting(_moduleId, "TileImageUrl", value); }
        }
    }
}